import {
    Entity,
    PrimaryGeneratedColumn,
    Column,
    CreateDateColumn,
    UpdateDateColumn,
    Index
} from 'typeorm';

export type TxStatus = 'queued' | 'pending' | 'sent' | 'confirmed' | 'failed' | 'reverted';
export type TxType = 'deploy_nft' | 'deploy_token' | 'mint_nft' | 'mint_token' | 'transfer' | 'approve' | 'other';

@Entity('tx_jobs')
export class TxJob {
    @PrimaryGeneratedColumn()
    id: number;

    @Column({ unique: true })
    @Index()
    requestId: string;

    @Column({ type: 'enum', enum: ['deploy_nft', 'deploy_token', 'mint_nft', 'mint_token', 'transfer', 'approve', 'other'], default: 'other' })
    type: TxType;

    @Column()
    to: string;

    @Column({ type: 'text' })
    data: string;

    @Column({ type: 'decimal', precision: 30, scale: 0, default: '0' })
    value: string;

    @Column({ type: 'int', nullable: true })
    gasLimit?: number;

    @Column({ type: 'decimal', precision: 20, scale: 0, nullable: true })
    gasPrice?: string;

    @Column({ type: 'enum', enum: ['queued', 'pending', 'sent', 'confirmed', 'failed', 'reverted'], default: 'queued' })
    status: TxStatus;

    @Column({ nullable: true })
    @Index()
    txHash?: string;

    @Column({ type: 'int', nullable: true })
    blockNumber?: number;

    @Column({ type: 'text', nullable: true })
    lastError?: string;

    @Column({ type: 'int', default: 0 })
    retryCount: number;

    @Column({ type: 'int', default: 3 })
    maxRetries: number;

    @Column({ type: 'json', nullable: true })
    metadata?: Record<string, any>;

    @CreateDateColumn()
    createdAt: Date;

    @UpdateDateColumn()
    updatedAt: Date;
}
